﻿//using Newtonsoft.Json;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Net.Http;
//using System.Net.Http.Headers;
//using System.Threading.Tasks;
//using WebApp.Models;

//namespace WebApp.Common
//{
//    public class WXHelper
//    {

//        public void SendMsg(string touser)
//        {

//            string accessToken = GetAccessToken();

//            HttpClient client = new HttpClient();
//            //SendMsgModel sendMsg = new SendMsgModel();
//            //sendMsg.touser = F;
//            //sendMsg.access_token = accessToken;
//            //WeappTemplateMsg weappTemplate = new WeappTemplateMsg();
//            //weappTemplate.data = new { keyword1=new { value="" }, keyword2=new { value =""} };
//            //weappTemplate.emphasis_keyword = "";
//            //weappTemplate.form_id = "";
//            //weappTemplate.page = "";
//            //weappTemplate.template_id = MyConfigurationSection.GetSite("WeappTemplate"); 
//            //sendMsg.weapp_template_msg = weappTemplate;
//            //MpTemplateMsg mpTemplate = new MpTemplateMsg();
//            //mpTemplate.appid = "";
//            //mpTemplate.data= new { keyword1 = new { value = "" }, keyword2 = new { value = "" } };
//            //mpTemplate.miniprogram = new { appid="", pagepath="" };
//            //mpTemplate.template_id = "";
//            //mpTemplate.url = "";
//            //sendMsg.mp_template_msg = mpTemplate;
//            var formid = _activityService.GetFormId();
//            TemplateMessage templateMessage = new TemplateMessage();
//            templateMessage.touser = touser;
//            templateMessage.access_token = accessToken;
//            templateMessage.form_id = formid;
//            templateMessage.page = page;
//            templateMessage.template_id = template_id;
//            templateMessage.emphasis_keyword = "";
//            templateMessage.data = new { keyword1 = new { value = "" }, keyword2 = new { value = "" } };

//            HttpContent httpContent = new StringContent(JsonConvert.SerializeObject(templateMessage));
//            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

//            HttpResponseMessage response = client.PostAsync("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=" + accessToken, httpContent).Result;
//            string result = response.Content.ReadAsStringAsync().Result;
//            client.Dispose();
//            TemplateResult access = Newtonsoft.Json.JsonConvert.DeserializeObject<TemplateResult>(result);
//            if (access.errcode == 0)
//            {
//                _activityService.UpdateForm(formid);
//            }
//        }
//        /// <summary>
//        /// 微信接口凭证
//        /// </summary>
//        /// <returns></returns>
//        private string GetAccessToken()
//        {
//            HttpClient client = new HttpClient();
//            HttpResponseMessage response = client.GetAsync("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret).Result;
//            string result = response.Content.ReadAsStringAsync().Result;
//            client.Dispose();
//            AccessModel access = Newtonsoft.Json.JsonConvert.DeserializeObject<AccessModel>(result);
//            if (access != null && access.errcode == 0)
//            {
//                return access.access_token;
//            }
//            return "";
//        }
//    }
//}
